//
//  LunBoPicture.m
//  UI0_轮播图
//
//  Created by dllo on 15/11/9.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "LunBoPicture.h"

@implementation LunBoPicture

- (void)dealloc
{
    [_array release];
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame withArray:(NSArray *)array;
{
    self = [super initWithFrame:frame];
    if (self) {
        self.array = [[NSMutableArray alloc] initWithArray:array];
        [self createScollView];
    }
    return self;
}

- (void)createScollView {
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    scrollView.pagingEnabled = YES;
    scrollView.scrollEnabled = YES;
    if (_array.count) {
        [_array insertObject:[self.array lastObject] atIndex:0];
        [_array addObject:self.array[1]];
        for (int i = 0; i < self.array.count; i++) {
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.frame.size.width * i, 0, self.frame.size.width, 200)];
            imageView.image = [UIImage imageNamed:self.array[i]];
            imageView.contentMode = YES;
            // 保持图片的比例不变充满屏幕
            imageView.contentMode = UIViewContentModeScaleAspectFill;
            // 超越边界不显示
            imageView.clipsToBounds = YES;
            [scrollView addSubview:imageView];
            [imageView release];
        }
    }
    scrollView.contentSize = CGSizeMake(self.frame.size.width * self.array.count, 0);
    scrollView.contentOffset = CGPointMake(self.frame.size.width, 0);
    scrollView.delegate = self;
    [self addSubview:scrollView];
    [scrollView release];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    if (scrollView.contentOffset.x == 0) {
        scrollView.contentOffset= CGPointMake(self.frame.size.width * (self.array.count - 2), 0);
    }
    
    if (scrollView.contentOffset.x == self.frame.size.width * (self.array.count - 1)) {
        scrollView.contentOffset = CGPointMake(self.frame.size.width, 0);
    }
}

@end
